package com.demo.amusic;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.demo.amusic.dao.MusicDao;
import com.demo.amusic.entity.Music;

import java.util.List;

public class PlayingList extends AppCompatActivity implements View.OnClickListener {
    List<Music> list = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setStatusBarColor(this,R.color.titleColor);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playing_list);
        initData();
        init();
    }

    ListView listView ;
    Button back,add;
    PlayingListAdapter adapter = null;
    private void init() {
        listView = findViewById(R.id.playing_list_list);
        back = findViewById(R.id.playing_list_back);
        add = findViewById(R.id.playing_list_add);
        back.setOnClickListener(this);
        add.setOnClickListener(this);

        adapter = new PlayingListAdapter(this,list,R.layout.playing_list_item);
        listView.setAdapter(adapter);
    }

    private void initData() {
        list = MusicDao.getInstance(this).list();
    }

    /**
     * 修改状态栏颜色，支持4.4以上版本
     * @param activity
     * @param colorId
     */
    public static void setStatusBarColor(Activity activity, int colorId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(activity.getResources().getColor(colorId));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.playing_list_back:
                finish();
                break;
            case R.id.playing_list_add:
                Intent intent = new Intent(PlayingList.this,EditActivity.class);
                intent.putExtra("data","new");
                startActivity(intent);
        }
    }

    @Override
    protected void onRestart() {
        reflush();
//        Toast.makeText(this,"restart",Toast.LENGTH_SHORT).show();
        super.onRestart();
    }

    private void reflush() {
        list.clear();
        list.addAll(MusicDao.getInstance(this).list());
        adapter.notifyDataSetChanged();

    }
}